<template>
  <a-modal
    title="新建菜单"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    :destroyOnClose="true"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="父级菜单"
        >
          <a-select placeholder="父级菜单" v-decorator="['pid']">
            <a-select-option value="-1">首页</a-select-option>
            <a-select-option value="">无</a-select-option>
          </a-select>
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="唯一标志符">
          <a-input
            placeholder="唯一标志符"
            v-decorator="['code', { rules: [{ required: true, message: '唯一标志符不能为空！' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="菜单名称">
          <a-input
            placeholder="菜单名称"
            v-decorator="['name', { rules: [{ required: true, message: '菜单名称不能为空！' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图标">
          <a-input placeholder="图标" v-decorator="['icon']" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="URL">
          <a-input placeholder="URL" v-decorator="['url', { rules: [{ required: true, message: 'URL不能为空！' }] }]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input
            placeholder="排序"
            v-decorator="['sort', { rules: [{ required: true, message: '排序不能为空！' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-select
            placeholder="状态"
            v-decorator="['status', { rules: [{ required: true, message: '状态不能为空！' }] }]"
          >
            <a-select-option value="1">正常</a-select-option>
            <a-select-option value="0">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="菜单是否显示">
          <a-select
            placeholder="菜单是否显示"
            v-decorator="['display', { rules: [{ required: true, message: '菜单是否显示不能为空！' }] }]"
          >
            <a-select-option value="1">显示</a-select-option>
            <a-select-option value="0">不显示</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="描述" hasFeedback>
          <a-textarea :rows="3" placeholder="描述" v-decorator="['description']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addMenus } from '@/api/menu'
export default {
  name: 'CreateModal',
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      // 向后端拉取可以用的操作列表
      permissionList: [
        { label: '新增', value: 'add', defaultChecked: false },
        { label: '查询', value: 'get', defaultChecked: false },
        { label: '修改', value: 'update', defaultChecked: false },
        { label: '列表', value: 'query', defaultChecked: false },
        { label: '删除', value: 'delete', defaultChecked: false },
        { label: '导入', value: 'import', defaultChecked: false },
        { label: '导出', value: 'export', defaultChecked: false }
      ],
      form: this.$form.createForm(this)
    }
  },

  // props: ['platTye'], // 接收父亲组建的值

  methods: {
    // 选中创建时间
    createdChange (value, dateString) {
      console.log('Formatted Selected Time: ', dateString)
    },
    // 选中创建时间
    updateChange (value, dateString) {
      console.log('Formatted Selected Time: ', dateString)
    },
    compareToFirstPassword (rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('password_hash')) {
        callback(new Error('两次输入密码不一致'))
        // callback('Two passwords that you enter is inconsistent!')
      } else {
        callback()
      }
    },
    validateToNextPassword (rule, value, callback) {
      const form = this.form
      if (value && this.confirmDirty) {
        form.validateFields(['confirm'], { force: true })
      }
      callback()
    },
    add () {
      this.visible = true
    },

    handleSubmit () {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        values.pid = -1
        // 删除对象中typeId
        delete values.confirm
        if (!errors) {
          console.log('values----', values)
          addMenus(values).then((res) => {
            if (res) {
              // 存储成功、刷新列表
              this.$emit('ok', values)
              this.visible = false
              this.confirmLoading = false
              this.$notification['success']({
                message: '提示',
                description: '数据添加成功！'
              })
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    }
  }
}
</script>
